package com.naicheng.ivote.views;

import java.util.LinkedList;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.naicheng.ivote.R;
import com.naicheng.ivote.constant.ConstantMine;
import com.naicheng.ivote.engin.BaseActivity;
import com.naicheng.ivote.utils.AppManager;
import com.naicheng.ivote.utils.MyPreferences;

public class GuidActivity extends BaseActivity {

	ViewPager viewpager;
	int position = 0;
	AppManager appManager = AppManager.getAppManager();
	private MyPreferences preference;
	List<View> list = new LinkedList<View>();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		setContentView(R.layout.guid_layout);
		viewpager = (ViewPager) this.findViewById(R.id.viewpager);
		preference = MyPreferences.getPrefer(this);
		addView();
		viewpager.setAdapter(new PagerAdapter() {

			@Override
			public int getCount() {
				return list.size();
			}

			@Override
			public boolean isViewFromObject(View arg0, Object arg1) {
				return arg0 == arg1;
			}

			@Override
			public Object instantiateItem(ViewGroup container, int position) {
				container.addView(list.get(position));// 添加页卡
				return list.get(position);
			}

			@Override
			public void destroyItem(ViewGroup container, int position,
					Object object) {
				container.removeView(list.get(position));
			}

		});
		super.onCreate(savedInstanceState);
	}

	private void addView() {
		View v1 = View.inflate(this, R.layout.activity_guidepage1, null);
		TextView tv5 = (TextView) v1.findViewById(R.id.guide1_text5);

		tv5.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				position = 1;
				System.out.println("position==========" + position);
				viewpager.scrollTo(position, 0);
			}
		});

		View v2 = View.inflate(this, R.layout.activity_guidepage2, null);
		TextView tv5_2 = (TextView) v2.findViewById(R.id.guide2_text5);

		tv5_2.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				position = 2;
				System.out.println("position==========" + position);
				viewpager.scrollTo(position, 0);
			}
		});

		View v3 = View.inflate(this, R.layout.activity_guidepage3, null);
		Button tv5_3 = (Button) v3.findViewById(R.id.guide3_but);

		tv5_3.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				startActivity(new Intent(GuidActivity.this, CommonActivity.class));
				preference.putBoolean(ConstantMine.KEY_IS_FIRST, false);
				appManager.finishActivity();
			}
		});
		list.add(v1);
		list.add(v2);
		list.add(v3);
	}

}
